using System;
using System.Collections.Generic;
using System.Text;

namespace LumiSoft.Net.MIME.Header
{
    /// <summary>
    /// Represents a MIME entity header field.
    /// </summary>
    public class MIME_HeaderField
    {
        private string                m_Name   = "";
        private MIME_HeaderFieldValue m_pValue = null;

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="name">Header field name.</param>
        /// <param name="value">Header field value.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>name</b> or <b>value</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public MIME_HeaderField(string name,string value)
        {
            if(name == null){
                throw new ArgumentNullException("name");
            }
            if(!IsValidName(name)){
                throw new ArgumentException("Invalid argument 'name' value.");
            }
            if(value == null){
                throw new ArgumentNullException("value");
            }

            m_Name   = name;
            m_pValue = new MIME_HeaderFieldValue();
            m_pValue.ValueString = value;
            //m_pValue = value;
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="name">Header field name.</param>
        /// <param name="value">Header field value.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>name</b> or <b>value</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public MIME_HeaderField(string name,MIME_HeaderFieldValue value)
        {
            if(name == null){
                throw new ArgumentNullException("name");
            }
            if(!IsValidName(name)){
                throw new ArgumentException("Invalid argument 'name' value.");
            }
            if(value == null){
                throw new ArgumentNullException("value");
            }

            m_Name   = name;
            m_pValue = value;
        }


        private bool IsValidName(string name)
        {
            return true;
        }

        private Type GetHeaderFieldValueType()
        {
            if(m_Name.ToLower() == "content-type"){
                return typeof(MIME_t_ContentType);
            }
            else if(m_Name.ToLower() == "content-disposition"){
                return typeof(MIME_t_ContentDisposition);
            }
            else{
                return typeof(MIME_HeaderFieldValue);
            }
        }


        #region override method ToString

        /// <summary>
        /// Returns header field as string value.
        /// </summary>
        /// <returns>Returns header field as string value.</returns>
        public override string ToString()
        {
            return m_Name + ":" + m_pValue.ValueString + "\r\n";
        }

        #endregion


        #region Properties Implementation

        /// <summary>
        /// Gets header field value.
        /// </summary>
        public string Name
        {
            get{ return m_Name; }
        }
        
        /// <summary>
        /// Gets or sets header feldvalue.
        /// </summary>
        /// <exception cref="ArgumentNullException">Is raised when null value is passed.</exception>
        public MIME_HeaderFieldValue Value
        {
            get{ return m_pValue; }

            set{
                if(value == null){
                    throw new ArgumentNullException("value");
                }

                m_pValue = value;
            }
        }
                
        #endregion

    }
}
